using System;
using System.Globalization;
using System.Text;
using PickGold.Charting;

namespace PickGold.Charting.Formulas
{
	internal abstract class FormulaInfo
	{
		private DataField[] _inputFields;
		private DataField[] _outputFields;
		private object[] _parameters;

		public FormulaInfo(DataField[] inputFields, DataField[] outputFields, params object[] defaultParams)
		{
			this._inputFields = inputFields;
			this._outputFields = outputFields;
			this._parameters = defaultParams;
		}

		internal virtual void CheckParameterString(string parameters)
		{
			if (!string.IsNullOrEmpty(parameters))
			{
				string[] strArray = parameters.Split(new char[] { ',' });
				int num = 0;
				for (int i = 0; (i < this._parameters.Length) && (num < strArray.Length); i++)
				{
					string str = strArray[num++];
					if (!string.IsNullOrEmpty(str))
					{
						try
						{
							this.ParseParameter(i, str);
						}
						catch (FormatException)
						{
							throw new ArgumentException(SR.ExceptionFormulaDataFormatInvalid(parameters));
						}
					}
				}
			}
		}

		internal virtual void LoadParametersFromString(string parameters)
		{
			if (!string.IsNullOrEmpty(parameters))
			{
				string[] strArray = parameters.Split(new char[] { ',' });
				int num = 0;
				for (int i = 0; (i < this._parameters.Length) && (num < strArray.Length); i++)
				{
					string str = strArray[num++];
					if (!string.IsNullOrEmpty(str))
					{
						this._parameters[i] = this.ParseParameter(i, str);
					}
				}
			}
		}

		internal virtual object ParseParameter(int index, string newParamValue)
		{
			object obj2 = this._parameters[index];
			if (obj2 is int)
			{
				return Convert.ToInt32(newParamValue, CultureInfo.InvariantCulture);
			}
			if (obj2 is bool)
			{
				return Convert.ToBoolean(newParamValue, CultureInfo.InvariantCulture);
			}
			if (obj2 is double)
			{
				return Convert.ToDouble(newParamValue, CultureInfo.InvariantCulture);
			}
			return null;
		}

		internal virtual string SaveParametersToString()
		{
			StringBuilder builder = new StringBuilder();
			for (int i = 0; i < this._parameters.Length; i++)
			{
				if (i > 0)
				{
					builder.Append(',');
				}
				builder.AppendFormat(CultureInfo.InvariantCulture, "{0}", new object[] { this._parameters[i] });
			}
			return builder.ToString();
		}

		public DataField[] InputFields
		{
			get
			{
				return this._inputFields;
			}
		}

		public DataField[] OutputFields
		{
			get
			{
				return this._outputFields;
			}
		}

		public object[] Parameters
		{
			get
			{
				return this._parameters;
			}
		}
	}
}

